<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "sys_config".
 *
 * @property integer $id
 * @property string $site_name
 * @property string $site_addr
 * @property string $site_reg_id
 * @property string $sxy_businessid
 * @property string $sxy_key
 * @property string $weixin_businessid
 * @property string $weixin_appid
 * @property string $weixin_appsecret
 * @property string $weixin_ipakey
 * @property string $weixin_cert
 * @property string $alipay_count
 * @property string $alipay_qrcode
 * @property string $logo_img
 * @property double $fenhong_rate
 * @property integer $is_fenhong_exec
 * @property integer $is_stop_fenhong
 * @property double $tjj_rate
 * @property double $tjj_one_rate
 * @property double $tjj_two_rate
 * @property double $tjj_three_rate
 * @property double $tjj_four_rate
 * @property double $tjj_five_rate
 * @property double $tjj_six_rate
 * @property double $tjj_seven_rate
 * @property double $level_one
 * @property double $level_one_reward
 * @property double $level_two
 * @property double $level_two_reward
 * @property double $level_three
 * @property double $level_three_reward
 * @property integer $is_stop_lindao
 * @property double $exchange_rate
 * @property double $congzhi_reward
 * @property integer $administrator
 * @property string $sms_user
 * @property string $sms_password
 * @property double $k_min_y
 * @property double $k_max_y
 * @property string $jyrs
 * @property string $jyl
 * @property string $pay_channel
 * @property string $useagreement
 * @property integer $forbid_tip
 * @property double $nea_count
 * @property double $usd_count
 */
class SysConfig extends \yii\db\ActiveRecord
{
  const FORBID_USER_TIP_WH=0;//维护
  const FORBID_USER_TIP_JS=1;//结算
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'sys_config';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['fenhong_rate', 'tjj_rate', 'tjj_one_rate', 'tjj_two_rate', 'tjj_three_rate', 'tjj_four_rate', 'tjj_five_rate', 'tjj_six_rate', 'tjj_seven_rate', 'level_one', 'level_one_reward', 'level_two', 'level_two_reward', 'level_three', 'level_three_reward', 'exchange_rate', 'congzhi_reward', 'k_min_y', 'k_max_y', 'jyrs', 'jyl', 'nea_count', 'usd_count'], 'number'],
            [['is_fenhong_exec', 'is_stop_fenhong', 'is_stop_lindao', 'administrator', 'forbid_tip'], 'integer'],
            [['is_stop_fenhong', 'tjj_rate', 'tjj_one_rate', 'tjj_two_rate', 'tjj_three_rate', 'tjj_four_rate', 'tjj_five_rate', 'tjj_six_rate', 'tjj_seven_rate', 'level_one', 'level_one_reward', 'level_two', 'level_two_reward', 'level_three', 'level_three_reward', 'is_stop_lindao', 'administrator', 'sms_user', 'sms_password'], 'required'],
            [['useagreement'], 'string'],
            [['site_name'], 'string', 'max' => 20],
            [['site_addr', 'alipay_qrcode', 'logo_img'], 'string', 'max' => 128],
            [['site_reg_id', 'alipay_count'], 'string', 'max' => 50],
            [['sxy_businessid', 'weixin_businessid', 'weixin_appid', 'weixin_cert'], 'string', 'max' => 32],
            [['sxy_key', 'weixin_appsecret', 'weixin_ipakey'], 'string', 'max' => 256],
            [['sms_user', 'sms_password'], 'string', 'max' => 30],
            [['pay_channel'], 'string', 'max' => 10],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'site_name' => 'Site Name',
            'site_addr' => 'Site Addr',
            'site_reg_id' => 'Site Reg ID',
            'sxy_businessid' => 'Sxy Businessid',
            'sxy_key' => 'Sxy Key',
            'weixin_businessid' => 'Weixin Businessid',
            'weixin_appid' => 'Weixin Appid',
            'weixin_appsecret' => 'Weixin Appsecret',
            'weixin_ipakey' => 'Weixin Ipakey',
            'weixin_cert' => 'Weixin Cert',
            'alipay_count' => 'Alipay Count',
            'alipay_qrcode' => 'Alipay Qrcode',
            'logo_img' => 'Logo Img',
            'fenhong_rate' => 'Fenhong Rate',
            'is_fenhong_exec' => 'Is Fenhong Exec',
            'is_stop_fenhong' => 'Is Stop Fenhong',
            'tjj_rate' => 'Tjj Rate',
            'tjj_one_rate' => 'Tjj One Rate',
            'tjj_two_rate' => 'Tjj Two Rate',
            'tjj_three_rate' => 'Tjj Three Rate',
            'tjj_four_rate' => 'Tjj Four Rate',
            'tjj_five_rate' => 'Tjj Five Rate',
            'tjj_six_rate' => 'Tjj Six Rate',
            'tjj_seven_rate' => 'Tjj Seven Rate',
            'level_one' => 'Level One',
            'level_one_reward' => 'Level One Reward',
            'level_two' => 'Level Two',
            'level_two_reward' => 'Level Two Reward',
            'level_three' => 'Level Three',
            'level_three_reward' => 'Level Three Reward',
            'is_stop_lindao' => 'Is Stop Lindao',
            'exchange_rate' => 'Exchange Rate',
            'congzhi_reward' => 'Congzhi Reward',
            'administrator' => 'Administrator',
            'sms_user' => 'Sms User',
            'sms_password' => 'Sms Password',
            'k_min_y' => 'K Min Y',
            'k_max_y' => 'K Max Y',
            'jyrs' => 'Jyrs',
            'jyl' => 'Jyl',
            'pay_channel' => 'Pay Channel',
            'useagreement' => 'Useagreement',
            'forbid_tip' => 'Forbid Tip',
            'nea_count' => 'Nea Count',
            'usd_count' => 'Usd Count',
        ];
    }
    /**
     * 更新推荐奖提成比例设置值
     */
    public static function getSysConfig()
    {
        $sql = 'SELECT * FROM sys_config';
        $sysconfig = SysConfig::findBySql($sql)->one();
        return $sysconfig;
    }

    /**
     * 更新推荐奖提成比例设置值
     */
    public static function updateTuijianRate($rate)
    {
        $sql = 'SELECT * FROM sys_config';
        $config = SysConfig::findBySql($sql)->one();
        $config->tjj_rate=$rate;
        if(!$config->save()){
          return false;
        }
        return true;
    }
    /**
     * 更新推荐奖提成比例设置值
     */
    public static function updateSevenTuijianRate($model)
    {
        $sql = 'SELECT * FROM sys_config';
        $config = SysConfig::findBySql($sql)->one();
        if(isset($model->one_rate)){
          $config->tjj_one_rate=$model->one_rate;
        }
        if(isset($model->two_rate)){
          $config->tjj_two_rate=$model->two_rate;
        }
        if(isset($model->three_rate)){
          $config->tjj_three_rate=$model->three_rate;
        }
        if(isset($model->four_rate)){
          $config->tjj_four_rate=$model->four_rate;
        }
        if(isset($model->five_rate)){
          $config->tjj_five_rate=$model->five_rate;
        }
        if(isset($model->six_rate)){
          $config->tjj_six_rate=$model->six_rate;
        }
        if(isset($model->seven_rate)){
          $config->tjj_seven_rate=$model->seven_rate;
        }
        if(!$config->save()){
          return false;
        }
        return true;
    }
    /**
     * 更新领导奖设置值
     */
    public static function updateLindaoReward($model)
    {
        $sql = 'SELECT * FROM sys_config';
        $config = SysConfig::findBySql($sql)->one();
        if(isset($model->level_one)){
          $config->level_one=$model->level_one;
        }
        if(isset($model->level_one_reward)){
          $config->level_one_reward=$model->level_one_reward;
        }
        if(isset($model->level_two)){
          $config->level_two=$model->level_two;
        }
        if(isset($model->level_two_reward)){
          $config->level_two_reward=$model->level_two_reward;
        }
        if(isset($model->level_three)){
          $config->level_three=$model->level_three;
        }
        if(isset($model->level_three_reward)){
          $config->level_three_reward=$model->level_three_reward;
        }
        if(isset($model->is_stop_lindao)){
          $config->is_stop_lindao=$model->is_stop_lindao;
        }

        if(!$config->save()){
          return false;
        }
        return true;
    }
    public function updateSysSetting($model){
      $sql = 'SELECT * FROM sys_config';
      $config = SysConfig::findBySql($sql)->one();
      $config->site_name=$model->site_name;
      $config->site_addr=$model->site_addr;
      $config->site_reg_id=$model->site_reg_id;
      $config->sms_user=$model->sms_user;
      $config->sms_password=$model->sms_password;
      $config->useagreement = $model->useagreement;
      if(isset($model->logo_img)&&!is_null($model->logo_img)){
        $config->logo_img=$model->logo_img;
      }
      if(!$config->save()){
        //print_r($config->errors);
        //exit;
        return false;
      }
      return true;
    }

    /**
     * 当前用户是否管理员
     */
    public static function curUserIsAdmin($model){

      if(Yii::$app->user->isGuest){
        return false;
      }
      if(is_null($model)){
        return false;
      }
      $userId=Yii::$app->user->getId();

      if($model->administrator==$userId){
        return true;
      }
      return false;
    }
    /**
     * 更新充值返还奖励设置值
     */
    public static function updateCongzhifanhuan($model)
    {
        $sql = 'SELECT * FROM sys_config';
        $config = SysConfig::findBySql($sql)->one();
        $config->exchange_rate=$model->exchange_rate;
        $config->congzhi_reward=$model->congzhi_reward;
        if(!$config->save()){
          return false;
        }
        return true;
    }

    /**
     * 更新静态分红设置
     */
    public static function updateFenhong($model)
    {
        $sql = 'SELECT * FROM sys_config';
        $config = SysConfig::findBySql($sql)->one();
        $config->fenhong_rate=$model->fenhong_rate;
        $config->is_fenhong_exec=$model->is_fenhong_exec;
        $config->is_stop_fenhong=$model->is_stop_fenhong;
        if(!$config->save()){
          return false;
        }
        return true;
    }

    /**
     * 更新充值通道设置
     */
    public static function updatePayChannel($model)
    {
        $sql = 'SELECT * FROM sys_config';
        $config = SysConfig::findBySql($sql)->one();
        $arr=[];
        $arr[0]=$model->sxy_checked;
        $arr[1]=$model->alipay_checked;
        $arr[2]=$model->weixin_checked;

        $config->pay_channel=implode(',',$arr);
        if(!$config->save()){
          return false;
        }
        return true;
    }

    /**
     * 更新充值通道设置
     */
    public static function updateSxyConfig($model)
    {
        $sql = 'SELECT * FROM sys_config';
        $config = SysConfig::findBySql($sql)->one();
        $config->sxy_businessid = $model->businessid;
        $config->sxy_key = $model->key;

        if(!$config->save()){
          return false;
        }
        return true;
    }

    /**
     * 更新充值通道设置
     */
    public static function updateWeixinConfig($model)
    {
        $sql = 'SELECT * FROM sys_config';
        $config = SysConfig::findBySql($sql)->one();
        $config->weixin_businessid = $model->businessid;
        $config->weixin_appid = $model->appid;
        $config->weixin_appsecret = $model->appsecret;
        $config->weixin_ipakey = $model->ipakey;
        $config->weixin_cert = $model->cert;
        $arr=explode(',',$config->pay_channel);
        if(count($arr)!=3){
          $arr[0]=0;
          $arr[1]=0;
          $arr[2]=0;
        }
        $arr[2]=($model->disable==1)? 0:1;
        $config->pay_channel = implode(',',$arr);

        if(!$config->save()){
          var_dump($config->errors);
          exit;
          return false;
        }
        return true;
    }

    /**
     * 更新充值通道设置
     */
    public static function updateAlipayConfig($model)
    {
        $sql = 'SELECT * FROM sys_config';
        $config = SysConfig::findBySql($sql)->one();
        $config->alipay_count = $model->count;
        $config->alipay_qrcode = $model->qrcode;

        $arr=explode(',',$config->pay_channel);
        if(count($arr)!=3){
          $arr[0]=0;
          $arr[1]=0;
          $arr[2]=0;
        }
        $arr[1]=($model->disable==1)? 0:1;
        $config->pay_channel = implode(',',$arr);

        if(!$config->save()){
          return false;
        }
        return true;
    }

    /**
     * 更新禁止用户提示
     */
    public static function updateForbidUserTip($mode)
    {
        $sql = 'SELECT * FROM sys_config';
        $config = SysConfig::findBySql($sql)->one();
        if($mode=='wh'){
          $config->forbid_tip=SysConfig::FORBID_USER_TIP_WH;
        }else if($mode=='js'){
          $config->forbid_tip=SysConfig::FORBID_USER_TIP_JS;
        }else {
          return false;
        }

        if(!$config->save()){
          return false;
        }
        return true;
    }

    /**
     * 资金互转-总账互转
     */
    public static function zijinHuzhuan($type,$money)
    {
        $sql = 'SELECT * FROM sys_config';
        $model = SysConfig::findBySql($sql)->one();
        if(is_null($model)){
          return false;
        }
        if($type==ChargeCashDeal::TYPE_NEA_USD){
          if($model->nea_count < $money){
            //nea账户金额不足，无法转出
            throw new Exception('总账基金账户金额不足，无法转出');
            return false;
          }
          if($money>$model->nea_count*0.7){
            //一次最多只能转出基金账户的70%
            throw new Exception('一次最多只能转出基金账户的70%');
            return false;
          }

          $model->nea_count-=$money;
          $model->usd_count+=$money;

        }else if($type==ChargeCashDeal::TYPE_USD_NEA){
          if($model->usd_count < $money){
            //usd账户金额不足，无法转出
            throw new Exception('总账个人账户金额不足，无法转出');
            return false;
          }
          $model->usd_count-=$money;
          $model->nea_count+=$money;
        }else{
          return false;
        }

        if(!$model->save()){
            return false;
        }  
        return true;
    }
}
